﻿<UserControl
    x:Class="XamlInspector.Presentation.PropertiesControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:presentation="using:XamlInspector.Presentation"
    xmlns:converters="using:XamlInspector.Presentation.Converters"
    xmlns:controls="using:XamlInspector.Presentation.Controls"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400" x:Name="root">
    <UserControl.Resources>

        <converters:PropertyToBindingRectConverter x:Key="PropertyToBindingRectConverter" />
        <presentation:AutoDataTemplateSelector x:Name="AutoDataTemplateSelector" />

        <ControlTemplate x:Key="PropertyTemplate" TargetType="ContentControl">
            <Grid Margin="0,2">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="16" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding Name}" Foreground="Black" HorizontalAlignment="Right" FontSize="14" VerticalAlignment="Center" />
                <ContentPresenter Grid.Column="1" />
                <Rectangle Width="10" Height="10" Grid.Column="2" Fill="{Binding Converter={StaticResource PropertyToBindingRectConverter}}" VerticalAlignment="Center" HorizontalAlignment="Center"
                           ToolTipService.ToolTip="{Binding Description}" />
            </Grid>
        </ControlTemplate>

        <DataTemplate x:Key="XamlProperty">
            <ContentControl Template="{StaticResource PropertyTemplate}">
                <TextBox Text="{Binding TextValue, Mode=TwoWay}" BorderBrush="{Binding Converter={StaticResource PropertyToBindingRectConverter}}" BorderThickness="1" FontSize="14" IsEnabled="{Binding IsEditable}" Height="28" />
            </ContentControl>
        </DataTemplate>

        <DataTemplate x:Key="XamlEnumProperty">
            <ContentControl Template="{StaticResource PropertyTemplate}">
                <ComboBox ItemsSource="{Binding Values}" BorderBrush="{Binding Converter={StaticResource PropertyToBindingRectConverter}}" BorderThickness="1" FontSize="14" IsEnabled="{Binding IsEditable}" Height="28"/>
            </ContentControl>
        </DataTemplate>

        <DataTemplate x:Key="XamlBooleanProperty">
            <ContentControl Template="{StaticResource PropertyTemplate}">
                <CheckBox IsChecked="{Binding Value, Mode=TwoWay}" BorderBrush="{Binding Converter={StaticResource PropertyToBindingRectConverter}}" BorderThickness="1" HorizontalAlignment="Left" Margin="0" Padding="0" IsEnabled="{Binding IsEditable}" Height="20" Width="20" />
            </ContentControl>
        </DataTemplate>

        <DataTemplate x:Key="XamlBrushProperty">
            <ContentControl Template="{StaticResource PropertyTemplate}">
                <Border Background="{Binding Brush}" Height="25" BorderThickness="1" BorderBrush="Gray" >
                    <TextBlock Text="{Binding BrushText}" Foreground="Black" />
                </Border>
            </ContentControl>
        </DataTemplate>

        <DataTemplate x:Key="XamlThicknessProperty">
            <ContentControl Template="{StaticResource PropertyTemplate}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <TextBlock Text="L" Foreground="Black" VerticalAlignment="Center" />
                    <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Left, Mode=TwoWay}" BorderThickness="1" BorderBrush="LightGray" />
                    <TextBlock Grid.Row="0" Grid.Column="2" Text="T" Foreground="Black"  VerticalAlignment="Center" />
                    <TextBox Grid.Row="0" Grid.Column="3" Text="{Binding Top, Mode=TwoWay}" BorderThickness="1" BorderBrush="LightGray" />
                    <TextBlock Grid.Row="1" Grid.Column="0" Text="R" Foreground="Black"  VerticalAlignment="Center" />
                    <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Right, Mode=TwoWay}" BorderThickness="1" BorderBrush="LightGray" />
                    <TextBlock Grid.Row="1" Grid.Column="2"  Text="B" Foreground="Black"  VerticalAlignment="Center" />
                    <TextBox Grid.Row="1" Grid.Column="3" Text="{Binding Bottom, Mode=TwoWay}" BorderThickness="1" BorderBrush="LightGray" />
                </Grid>
            </ContentControl>
        </DataTemplate>

        <DataTemplate x:Key="XamlCompositeProperty">
            <controls:LazyExpander>
                <controls:LazyExpander.Header>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="16" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding Name}" Foreground="Black" HorizontalAlignment="Right" FontSize="14" />
                        <TextBox Grid.Column="1" Text="{Binding TextValue, Mode=TwoWay}" BorderBrush="{Binding Converter={StaticResource PropertyToBindingRectConverter}}" BorderThickness="1" FontSize="14" IsEnabled="False" />
                        <Rectangle Width="10" Height="10" Grid.Column="2" Fill="{Binding Converter={StaticResource PropertyToBindingRectConverter}}" VerticalAlignment="Center" HorizontalAlignment="Center"
                                   ToolTipService.ToolTip="{Binding Description}"/>
                    </Grid>
                </controls:LazyExpander.Header>
                <ItemsControl ItemsSource="{Binding Properties}" HorizontalContentAlignment="Stretch" ItemTemplateSelector="{StaticResource AutoDataTemplateSelector}" />
            </controls:LazyExpander>
        </DataTemplate>

        <Style TargetType="ListViewItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListViewItem">
                        <Grid Margin="4" >
                            <ContentPresenter Content="{TemplateBinding Content}" VerticalAlignment="Center" Margin="10,0" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <TextBox Margin="5" BorderThickness="1" BorderBrush="LightGray" Text="{Binding Filter, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
        <StackPanel Grid.Column="1" Grid.Row="0" Orientation="Horizontal">
            <ToggleButton IsChecked="{Binding ShowLocalValuesOnly, Mode=TwoWay}" Content="Local" />
            <ToggleButton IsChecked="{Binding ShowChangedValuesOnly, Mode=TwoWay}" Content="Changed" />
        </StackPanel>
        <ListView Grid.Row="1" Grid.Column="0"  Grid.ColumnSpan="2" x:Name="properties" HorizontalContentAlignment="Stretch" ItemTemplateSelector="{StaticResource AutoDataTemplateSelector}" SelectionMode="None">
        </ListView>
    </Grid>
</UserControl>
